﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Server
{
    public partial class ScreenFrame : Form
    {
        private Point _start;
        private Size _size;

        public Point CaptureStart
        {
            get { return _start; }
        }

        public Size CaptureSize
        {
            get { return _size; }
        }

        private Point nwPoint;
        private Point wPoint;
        private Point swPoint;
        private Point sPoint;
        private Point sePoint;
        private Point ePoint;
        private Point nePoint;
        private Point nPoint;
        private Point cPoint;

        private ConfirmScreen confirmScreen;
        private bool confirmLoaded = false;

        public ScreenFrame()
        {
            InitializeComponent();
            moveButton.Image = Properties.Resources.Bussola.ToBitmap();
        }

        private void ScreenFrame_Load(object sender, EventArgs e)
        {
            confirmScreen = new ConfirmScreen();
            confirmScreen.OKButton.MouseClick += new MouseEventHandler(Confirm_OKButton_MouseClick);
            confirmScreen.CancButton.MouseClick += new MouseEventHandler(Confirm_CancelButton_MouseClick);
            computeConfirmLocation();
            confirmScreen.Show();
            confirmLoaded = true;
            computeConfirmLocation();
        }

        private void computeConfirmLocation()
        {
            if (confirmLoaded)
            {
                Point newLoc = new Point();
                //di default va sotto al frame
                newLoc.Y = this.Location.Y + this.Height + 10;
                newLoc.X = this.Location.X;
                if (newLoc.X + confirmScreen.Width > Screen.PrimaryScreen.Bounds.Right)
                    newLoc.X = Screen.PrimaryScreen.Bounds.Right - confirmScreen.Width;

                if ((Location.Y + Height + confirmScreen.Height + 10) > Screen.PrimaryScreen.Bounds.Bottom &&
                    (Location.Y - confirmScreen.Height - 10) < Screen.PrimaryScreen.Bounds.Top)
                {
                    // va inserito all'interno di questo form, o a destra, o a sinistra
                    newLoc.X = this.Location.X + this.Width - 10 - confirmScreen.Width;
                    if (newLoc.X < Screen.PrimaryScreen.Bounds.Left)
                        newLoc.X = this.Location.X + this.Width + 10;
                    else if ((newLoc.X + confirmScreen.Width) > Screen.PrimaryScreen.Bounds.Right)
                        newLoc.X = this.Location.X - 10 - confirmScreen.Width;
                    newLoc.Y = this.Location.Y + this.Height - 10 - confirmScreen.Height;
                    confirmScreen.BringToFront();
                }
                else if ((this.Location.Y + this.Height + confirmScreen.Height + 10) > Screen.PrimaryScreen.Bounds.Bottom)
                {
                    //va messo sopra
                    newLoc.Y = this.Location.Y - confirmScreen.Height - 10;
                    newLoc.X = this.Location.X;
                    if (newLoc.X + confirmScreen.Width > Screen.PrimaryScreen.Bounds.Right)
                        newLoc.X = Screen.PrimaryScreen.Bounds.Right - confirmScreen.Width;
                }
                confirmScreen.Location = newLoc;
            }
        }

        private void Confirm_OKButton_MouseClick(object sender, EventArgs e)
        {
            confirmScreen.Dispose();
            confirmScreen.Close();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void Confirm_CancelButton_MouseClick(object sender, EventArgs e)
        {
            confirmScreen.Dispose();
            confirmScreen.Close();
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ScreenFrame_Paint(object sender, PaintEventArgs e)
        {
            //ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Lime, ButtonBorderStyle.Solid); //eventualmente si possono anche specificare le ampiezze dei bordi
            HatchBrush hb = new HatchBrush(HatchStyle.Percent80, this.TransparencyKey);
            e.Graphics.FillRectangle(hb, DisplayRectangle);
            ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Lime, 8, ButtonBorderStyle.Solid,
                Color.Lime, 8, ButtonBorderStyle.Solid,
                Color.Lime, 8, ButtonBorderStyle.Solid,
                Color.Lime, 8, ButtonBorderStyle.Solid);
        }

        private void ScreenFrame_Resize(object sender, EventArgs e)
        {
            draggableRectangleControlNW.Location = new Point(0, 0);
            draggableRectangleControlW.Location = new Point(0, Height / 2 - 4);
            draggableRectangleControlSW.Location = new Point(0, Height - 8);
            draggableRectangleControlS.Location = new Point(Width / 2 - 4, Height - 8);
            draggableRectangleControlSE.Location = new Point(Width - 8, Height - 8);
            draggableRectangleControlE.Location = new Point(Width - 8, Height / 2 - 4);
            draggableRectangleControlNE.Location = new Point(Width - 8, 0);
            draggableRectangleControlN.Location = new Point(Width / 2 - 4, 0);

            moveButton.Location = new Point(Width / 2 - moveButton.Width / 2, Height / 2 - moveButton.Height / 2);
            _size = Size;
            computeConfirmLocation();
            Invalidate();
        }

        private void ScreenFrame_Move(object sender, EventArgs e)
        {
            _start = Location;
            computeConfirmLocation();
        }

        /* miei draggable rectangle */

        /* NW */
        private void draggableRectangleControlNW_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNWSE;
        }

        private void draggableRectangleControlNW_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlNW_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                nwPoint = new Point(e.X, e.Y);
                draggableRectangleControlNW.Capture = true;
            }
        }

        private void draggableRectangleControlNW_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - nwPoint.Y;
                int diffX = e.X - nwPoint.X;
                Size newSize = new Size(Width - diffX, Height - diffY);
                Point newLoc = new Point(Location.X + diffX, Location.Y + diffY);
                if (newSize.Width < MinimumSize.Width)
                    newLoc.X = Location.X;
                if (newSize.Height < MinimumSize.Height)
                    newLoc.Y = Location.Y;
                Size = newSize;
                Location = newLoc;
            }
        }

        private void draggableRectangleControlNW_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlNW.Capture = false;
            }
        }

        /* W */
        private void draggableRectangleControlW_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeWE;
        }

        private void draggableRectangleControlW_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlW_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                wPoint = new Point(e.X, e.Y);
                draggableRectangleControlW.Capture = true;
            }
        }

        private void draggableRectangleControlW_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffX = e.X - wPoint.X;
                Size newSize = new Size(Width - diffX, Height);
                Point newLoc = new Point(Location.X + diffX, Location.Y);
                if (newSize.Width >= MinimumSize.Width)
                {
                    Size = newSize;
                    Location = newLoc;
                }
            }
        }

        private void draggableRectangleControlW_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlW.Capture = false;
            }
        }

        /* SW */
        private void draggableRectangleControlSW_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNESW;
        }

        private void draggableRectangleControlSW_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlSW_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                swPoint = new Point(e.X, e.Y);
                draggableRectangleControlSW.Capture = true;
            }
        }

        private void draggableRectangleControlSW_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - swPoint.Y;
                int diffX = e.X - swPoint.X;
                Size newSize = new Size(Width - diffX, Height + diffY);
                Point newLoc = new Point(Location.X + diffX, Location.Y);
                if (newSize.Width < MinimumSize.Width)
                    newLoc.X = Location.X;
                Size = newSize;
                Location = newLoc;
            }
        }

        private void draggableRectangleControlSW_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlSW.Capture = false;
            }
        }

        /* S */
        private void draggableRectangleControlS_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNS;
        }

        private void draggableRectangleControlS_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlS_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                sPoint = new Point(e.X, e.Y);
                draggableRectangleControlS.Capture = true;
            }
        }

        private void draggableRectangleControlS_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - sPoint.Y;
                Size newSize = new Size(Width, Height + diffY);
                if (newSize.Height >= MinimumSize.Height)
                    Size = newSize;
            }
        }

        private void draggableRectangleControlS_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlS.Capture = false;
            }
        }

        /* SE */
        private void draggableRectangleControlSE_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNWSE;
        }

        private void draggableRectangleControlSE_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlSE_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                sePoint = new Point(e.X, e.Y);
                draggableRectangleControlSE.Capture = true;
            }
        }

        private void draggableRectangleControlSE_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - sePoint.Y;
                int diffX = e.X - sePoint.X;
                Size newSize = new Size(Width + diffX, Height + diffY);
                Size = newSize;
            }
        }

        private void draggableRectangleControlSE_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlSE.Capture = false;
            }
        }

        /* E */
        private void draggableRectangleControlE_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeWE;
        }

        private void draggableRectangleControlE_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlE_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ePoint = new Point(e.X, e.Y);
                draggableRectangleControlE.Capture = true;
            }
        }

        private void draggableRectangleControlE_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffX = e.X - ePoint.X;
                Size newSize = new Size(Width + diffX, Height);
                if (newSize.Width >= MinimumSize.Width)
                    Size = newSize;
            }
        }

        private void draggableRectangleControlE_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlE.Capture = false;
            }
        }

        /* NE */
        private void draggableRectangleControlNE_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNESW;
        }

        private void draggableRectangleControlNE_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlNE_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                nePoint = new Point(e.X, e.Y);
                draggableRectangleControlNE.Capture = true;
            }
        }

        private void draggableRectangleControlNE_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - nePoint.Y;
                int diffX = e.X - nePoint.X;
                Size newSize = new Size(Width + diffX, Height - diffY);
                Point newLoc = new Point(Location.X, Location.Y + diffY);
                if (newSize.Height < MinimumSize.Height)
                    newLoc.Y = Location.Y;
                Size = newSize;
                Location = newLoc;
            }
        }

        private void draggableRectangleControlNE_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlNE.Capture = false;
            }
        }

        /* N */
        private void draggableRectangleControlN_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeNS;
        }

        private void draggableRectangleControlN_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void draggableRectangleControlN_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                nPoint = new Point(e.X, e.Y);
                draggableRectangleControlN.Capture = true;
            }
        }

        private void draggableRectangleControlN_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int diffY = e.Y - nPoint.Y;
                Size newSize = new Size(Width, Height - diffY);
                Point newLoc = new Point(Location.X, Location.Y + diffY);
                if (newSize.Height < MinimumSize.Height)
                    newLoc.Y = Location.Y;
                Size = newSize;
                Location = newLoc;
            }
        }

        private void draggableRectangleControlN_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                draggableRectangleControlN.Capture = false;
            }
        }


        /* CENTRAL BUTTON */
        private void moveButton_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeAll;
        }

        private void moveButton_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void moveButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                cPoint = new Point(e.X, e.Y);
                moveButton.Capture = true;
            }
        }

        private void moveButton_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point newLoc = new Point(Location.X + e.X - cPoint.X, Location.Y + e.Y - cPoint.Y);
                Size newSize = Size;
                if ((newLoc.X + newSize.Width) > Screen.PrimaryScreen.Bounds.Right)
                {
                    newSize.Width = Screen.PrimaryScreen.Bounds.Right - newLoc.X;
                }
                else if (newLoc.X < Screen.PrimaryScreen.Bounds.Left)
                {
                    newSize.Width -= (Screen.PrimaryScreen.Bounds.Left - newLoc.X);
                    newLoc.X = Screen.PrimaryScreen.Bounds.Left;
                }

                if ((newLoc.Y + newSize.Height) > Screen.PrimaryScreen.Bounds.Bottom)
                    newSize.Height = Screen.PrimaryScreen.Bounds.Bottom - newLoc.Y;
                else if (newLoc.Y < Screen.PrimaryScreen.Bounds.Top)
                {
                    newSize.Height -= (Screen.PrimaryScreen.Bounds.Top - newLoc.Y);
                    newLoc.Y = Screen.PrimaryScreen.Bounds.Top;
                }
                if (newSize.Height < MinimumSize.Height)
                    newLoc.Y = Location.Y;
                if (newSize.Width < MinimumSize.Width)
                    newLoc.X = Location.X;
                Size = newSize;
                Location = newLoc;
            }
        }

        private void moveButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                moveButton.Capture = false;
            }
        }

        private void ScreenFrame_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!confirmScreen.IsDisposed)
            {
                confirmScreen.Dispose();
                confirmScreen.Close();
            }
        }
    }
}
